<?php
/**
 * Created by PhpStorm.
 * User: mww
 * 好友
 */
namespace app\api\controller;
use think\Db;
class Tang extends Base {

    /**
     * 异地登录验证
     * @return string|void
     */
    public function _initialize()
    {
        extract(input());
        if($user_id != 0){
            $auth = $this->checkAll($user_id,$time,$value,$reqKey);
            if($auth == 112){
                echo json_encode(['status'=>301,'msg'=>$this->otherLogin]);
                exit;
            }elseif($auth == 110){
                echo json_encode(['status' => 302, 'msg' => $this->frost]);
                exit;
            }elseif($auth == 1103){
                echo json_encode(['status' => 303, 'msg' => $this->del]);
                exit;
            }
        }

    }
    /**
     * 给老汤的，聊天用：根据id返回信息
     */
    public function byIdsGetInfos()
    {
        $uids = trim($_POST['id'],',');
        $pd = Db::table('up_u_user')
            ->where('user_id','in',$uids)
            ->field('user_username,user_id,user_image,user_hospital,user_room,user_job_desc,user_type')
            ->select();
        foreach ($pd as $k => $v){
            if($v['user_type'] == 2){
                $pd[$k]['user_hospital'] = Db::table('up_company')
                    ->where('c_id',$v['user_hospital'])
                    ->value('c_name');
                $pd[$k]['user_room'] = Db::table('up_company_team')
                    ->where('ct_id',$v['user_room'])
                    ->value('ct_name');
            }
        }
        return json(['status'=>200,'msg'=>$pd]);
    }
    /**
     * 给老汤的，聊天用：这个id是不是我的好友
     */
    public function isMyFriend()
    {
        extract(input());
        $one = Db::table('up_friend')
            ->where('friend_user_id',$user_id)
            ->where('friend_other_user',$uid)
            ->find();
        $status = 0;
        if($one && $one['friend_status'] == 2){
            $status = 1;
        }
        return json(['status'=>200,'msg'=>$status]);
    }

    /**
     * tang(这些id跟我的关系)
     */
    public function docIdsWithMe()
    {
        extract(input());
        $user = $this->getUserInfo($user_id,'user_type,user_hospital,user_room',2);
        $datas = [];
        $datas_group = [];
        if(isset($ids) && $ids){
            $datas = Db::table('up_u_user')
                ->where('user_id','in',trim($ids,','))
                ->field('user_username,user_id,user_image,user_hospital,user_room,user_job_desc,user_type')
                ->select();
            foreach ($datas as $k => $v){
                if($user['user_type'] == 1){
                    if($v['user_type'] == $user['user_type']){
                        if($v['user_hospital'] == $user['user_hospital']){
                            $datas[$k]['relation'] = 2;
                        }else{
                            $datas[$k]['relation'] = 1;
                        }
                    }else{
                        $datas[$k]['relation'] = 3;
                    }
                }else{
                    if($v['user_type'] == $user['user_type']){
                        $datas[$k]['relation'] = 1;
                    }else{
                        $datas[$k]['relation'] = 3;
                    }
                }
                if($v['user_type'] == 2){
                    $datas[$k]['user_hospital'] = Db::table('up_company')
                        ->where('c_id',$v['user_hospital'])
                        ->value('c_name');
                    $datas[$k]['user_room'] = Db::table('up_company_team')
                        ->where('ct_id',$v['user_room'])
                        ->value('ct_name');
                }
            }
        }
        if(isset($gids) && $gids){
            $datas_group = Db::table('up_group')->alias('g')
                ->join('up_u_user u','g.g_user_id = u.user_id','left')
                ->where('g_id','in',trim($gids,','))
                ->field('g_id,g_name,g_thumb,g_user_id,user_type,user_hospital')
                ->select();
            foreach ($datas_group as $k => $v){
                if($user['user_type'] == 1){
                    //我是医生: 1同行  2同院  3不同类型
                    if($user['user_type'] == $v['user_type']){
                        if($user['user_hospital'] == $v['user_hospital']){
                            $datas_group[$k]['new_type'] = 2;
                        }else{
                            $datas_group[$k]['new_type'] = 1;
                        }
                    }else{
                        $datas_group[$k]['new_type'] = 3;
                    }
                }else{
                    //我是业务员: 1同行  2同院  3不同类型
                    if($user['user_type'] == $v['user_type']){
                        $datas_group[$k]['new_type'] = 1;
                    }else{
                        $datas_group[$k]['new_type'] = 3;
                    }
                }
            }
        }

        return json(['status'=>200,'msg'=>$datas,'msg_group'=>$datas_group]);
    }
    /**
     * 群语音：创建
     */
    public function groupRadioVideo()
    {
        $user_id = $_POST['user_id'];
        $g_id = $_POST['g_id'];
        $type = $_POST['type'];
        $uid = explode(',',trim($_POST['uid'],','));
        //----------------------------------------------------------------------------
        $have = Db::table('up_group_user_status')
            ->where('g_id',$g_id)
            ->where('g_type',$type)
            ->where('g_status','in','1,2')
            ->find();
        if($have){
            return json(['status'=>100,'msg'=>'有未结束的群']);
        }
        //----------------------------------------------------------------------------
        $today = date('Y-m-d H:i:s');
        $data = [];
        foreach ($uid as $k => $v){
            $g_status = 1;
            if($v == $user_id) $g_status = 2;
            $data[] = [
                'g_id' => $g_id,
                'g_user_id' => $v,
                'g_status' => $g_status,
                'g_type' => $type,
                'g_time' => $today
            ];
        }
        $res = Db::table('up_group_user_status')->insertAll($data);
        if($res){
            return json(['status'=>200,'msg'=>'初始化成功']);
        }else{
            return json(['status'=>100,'msg'=>'初始化失败']);
        }
    }
    /**
     * 群语音：状态查询
     */
    public function groupRadioVideoStatus()
    {
        $g_id = $_POST['g_id'];
        $type = $_POST['type'];
        $data = Db::table('up_group_user_status')
            ->where('g_id',$g_id)
            ->where('g_type',$type)
            ->where('g_status','in','1,2')
            ->field('g_user_id,g_status')
            ->select();
        return json(['status'=>200,'msg'=>$data]);
    }
    /**
     * 群语音状态
     * status：1 加入    2 是同意      3 是出去
     */
    public function groupRadioVideoUpdate()
    {
        $user_id = $_POST['user_id'];
        $g_id = $_POST['g_id'];
        $type = $_POST['type'];
        $status = $_POST['status'];
        $today = date('Y-m-d H:i:s');
        $data = Db::table('up_group_user_status')
            ->where('g_id',$g_id)
            ->where('g_type',$type)
            ->where('g_user_id',$user_id)
            ->order('g_time desc')
            ->find();
        //1、拉人进来
        //2、同意或主动加入
        //3、退出
        if($data){
            $update = [
                'g_status' => $status,
                'g_time' => $today,
            ];
            $res = Db::table('up_group_user_status')
                ->where('g_k_id',$data['g_k_id'])
                ->update($update);
        }else{
            $update = [
                'g_id' => $g_id,
                'g_user_id' => $user_id,
                'g_status' => $status,
                'g_type' => $type,
                'g_time' => $today,
            ];
            $res = Db::table('up_group_user_status')->insert($update);
        }
        if($status == 3){
            $count1 = Db::table('up_group_user_status')
                ->where('g_id',$g_id)
                ->where('g_type',$type)
                ->where('g_status',2)
                ->count();
            $count2 = Db::table('up_group_user_status')
                ->where('g_id',$g_id)
                ->where('g_type',$type)
                ->where('g_status','in','1,2')
                ->count();
            if($count1 <= 0 || $count2 <= 1){
                Db::table('up_group_user_status')
                    ->where('g_id',$g_id)
                    ->where('g_type',$type)
                    ->where('g_status','in','1,2')->update([
                        'g_status' => 3,
                        'g_time' => $today,
                    ]);
            }
        }
        return json(['status'=>200,'msg'=>'ok']);
        if($res){
            return json(['status'=>200,'msg'=>'ok']);
        }else{
            return json(['status'=>100,'msg'=>'error']);
        }
    }
    /**
     * 根据群id获取个用户的状态（是语音还是视频）
     * 返参：g_type：1语音  2视频
     */
    public function groupUserStatus()
    {
        $g_id = $_POST['g_id'];
        $data = Db::table('up_group_user_status')
            ->where('g_id',$g_id)
            ->where('g_status','in','1,2')
            ->field('count(*) num,g_type')
            ->find();
        return json(['status'=>200,'msg'=>$data]);
    }
}